package com.tabjin.rfidsocket.controller.v1.sys;

import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.pojo.bo.SysDeptBO;
import com.tabjin.rfidsocket.pojo.dto.DeptLevelDTO;
import com.tabjin.rfidsocket.pojo.vo.SysDeptVO;
import com.tabjin.rfidsocket.service.sys.SysDeptService;
import com.tabjin.rfidsocket.service.sys.SysTreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tabjin
 * create at 2020/12/8 11:11
 * @program rfid_socket
 * @description 部门控制器
 */
@RestController
@RequestMapping(value = "/sys/dept")
@Slf4j
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysTreeService sysTreeService;

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView page() {
        return new ModelAndView("dept");
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult addDept(@RequestBody SysDeptBO sysDeptBO) {
        SysDeptVO vo = sysDeptService.add(sysDeptBO);
        return TabJsonResult.ok(vo);
    }

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult tree() {
        List<DeptLevelDTO> dtoList = sysTreeService.deptTree();
        return TabJsonResult.ok(dtoList);
    }

    @RequestMapping(value = "/tree/{id}", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult treeWithRootId(@PathVariable(value = "id") Integer id) {
        List<DeptLevelDTO> dtoList = sysTreeService.deptTree();
        List<DeptLevelDTO> collect = dtoList.stream().filter(item -> item.getId() == id).collect(Collectors.toList());
        return TabJsonResult.ok(collect);
    }

    @RequestMapping(value = "/depts/{id}", method = RequestMethod.GET)
    @ResponseBody
    public TabJsonResult deptsWithRootId(@RequestParam(value = "id") Integer rootId) {
        List<DeptLevelDTO> dtoList = sysTreeService.deptTree();
        List<DeptLevelDTO> collect = dtoList.stream().filter(item -> item.getId() == rootId).collect(Collectors.toList());
        return TabJsonResult.ok(collect);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult updateDept(@RequestBody SysDeptBO sysDeptBO) {
        sysDeptService.update(sysDeptBO);
        return TabJsonResult.ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult delete(@RequestParam(value = "id") Integer id) {
        // TODO
        return TabJsonResult.ok();
    }
}
